%% Training and Testing MULTI Action Recognition in the Weizmann Dataset
%UsingEuclidean Distance Transform

clear all
close all
clc


%NICTA && Server
addpath('/home/johanna/toolbox/libsvm-3.20/matlab')
addpath('/home/johanna/toolbox/yael/matlab');

%Home
addpath('/media/johanna/HD1T/Toolbox/libsvm-3.20/matlab');
addpath('/media/johanna/HD1T/Toolbox/yael/matlab');


% Si cambias de Ng, correr los sgtes. dos archivos:
is_pca = false;
Ncent = 2;
%display('Calculating FV for Training');
%FV_weizmann_training_edt(Ncent, is_pca);

Ng = int2str(Ncent);


ACC = [];
all_prediction = [];
all_real = [];
actionNames = importdata('actionNames.txt');
n_actions = length(actionNames);
%% TRAINING
%  Loading Training data
for r=1:9
    run = int2str(r);
    %fprintf('Running for %s \n', run);
    people_train = importdata(strcat('./run', run, '/train_list_run', run, '.dat'));
    n_pe_tr  = length(people_train);
    
    data_train = [];
    labels_train = [];
    
    
    display('TRAINING');
    fprintf('Running for %s \n', run);
    for pe = 1: n_pe_tr
        for act = 1:n_actions
            if (is_pca)
                load_name = strcat('./run', run,  '/FV_training/pca_edt_FV_', people_train(pe),'_',actionNames(act), '_Ng', Ng, '.txt');
            else
                load_name = strcat('./run', run,  '/FV_training/edt_FV_', people_train(pe),'_',actionNames(act), '_Ng', Ng, '.txt');
            end
            sLoad = char(load_name);
            FV = load(sLoad);
            data_train = [data_train FV];
            labels_train = [labels_train (act - 1)];
        end
    end
    
    
    data_train = data_train';
    labels_train = labels_train';
    model = svmtrain(labels_train, data_train, ['-s 0 -t 0 -b 1' ]);
    
    if (is_pca)
        save_name = strcat('./run', run,  '/pca_svm_model');
    else
        save_name = strcat('./run', run,  '/svm_model');
    end
    
    sSave= char(save_name);
    display('Saving Model...');
    save(sSave, 'model');
end


%% TESTING. 
%  Loading Testing data

ACC = [];
L =25;
for r=1:9
    run = int2str(r);
    people_test = importdata(strcat('./run', run, '/test_list_run', run, '.dat'));
    n_pe_te  = length(people_test);
    fprintf('RUN %d \n', r);
    display('Loading SVM Model'); % loading 'model'
    
    if (is_pca)
        load_name = strcat('./run', run,  '/pca_svm_model');
    else
        load_name = strcat('./run', run,  '/svm_model');
    end
    
    sLoad= char(load_name);
    load(sLoad);
    
    if (is_pca)
        
        w  =    load(strcat('./run', run, '/edt_universal_GMM/pca_edt_weights_Ng', Ng ));
        mu =    load(strcat('./run', run, '/edt_universal_GMM/pca_edt_means_Ng'  , Ng ));
        sigma = load(strcat('./run', run, '/edt_universal_GMM/pca_edt_covs_Ng'   , Ng ));
        
        trans_matrix_pca = load(strcat('./run', run, '/edt_universal_GMM/trans_matrix_pca.dat' ));
    else
        w  =    load(strcat('./run', run, '/edt_universal_GMM/edt_weights_Ng',Ng));
        mu =    load(strcat('./run', run, '/edt_universal_GMM/edt_means_Ng',Ng  ));
        sigma = load(strcat('./run', run, '/edt_universal_GMM/edt_covs_Ng',Ng   ));
    end
    
    test_info =  cell(n_pe_te,4);
    k=1;
    
    for pe = 1:n_pe_te
        %display(strcat(people_test(pe),'_d', sc));
        load_name = strcat('./edt_multi_features_testing/edt_feat_', people_test(pe), '.dat');
        sLoad = char(load_name);
        feat_video = load(sLoad);
        
        
        if (is_pca)
            size( trans_matrix_pca )
            size(feat_video)
            feat_video = feat_video'*trans_matrix_pca;
            feat_video = feat_video';
            size(feat_video)
            pause()
        end
        
        
        
        %Loading labels. In a frame basis
        load_name_lab = strcat('./edt_multi_features_testing/edt_lab_feat_', people_test(pe), '.dat');
        sLoad_lab = char(load_name_lab);
        real_labels = load(sLoad_lab);
        est_labels  = ones (length(real_labels),1)*(-1);
        ini = 1;
        fin = ini + L;
        more = true;
        while(more) %
            [pre_label pre_prob_label] = classify_segment_edt(ini, fin, feat_video, w, mu, sigma, model);
            fin = fin + 1;
            [label prob_label] = classify_segment_edt(ini, fin, feat_video, w, mu, sigma, model);
            
            while (label == pre_label) && ( prob_label> pre_prob_label) && ( fin+1< length(real_labels))
                %display([ini fin]);
                pre_label = label;
                pre_prob_label = prob_label;
                fin = fin + 1;
                [label prob_label] = classify_segment_edt(ini, fin, feat_video, w, mu, sigma, model);
            end
            
            if (ini >= length(real_labels) )
                more =false;
            elseif ( fin > length(real_labels) )
                fin = length(real_labels);
                [label prob_label] = classify_segment_edt(ini, fin, feat_video, w, mu, sigma, model);
                est_labels (ini:length(real_labels)) = label;
                more =false;
            else
                est_labels (ini:fin) = pre_label;
                ini = fin;
                fin = ini + L;
                if (fin >= length(real_labels) ) %Agregar a kth
                    fin = length(real_labels);
                    [label prob_label] = classify_segment_edt(ini, fin, feat_video, w, mu, sigma, model);
                    est_labels (ini:length(real_labels)) = label;
                    more =false;
                end
                %display('Que caso es este???');
            end
        end
        
        is_negative = est_labels <0;
        is_negative = est_labels(is_negative);
        if (length(is_negative) > 0 )
            display('Que pasa Aqui???') ;
            pause
        end
        
        acc = sum(est_labels == real_labels)*100/length(real_labels);
        ACC =  [ACC acc];
        test_info{k,1} = strcat(people_test(pe));
        test_info{k,2} = real_labels;
        test_info{k,3} = est_labels;
        test_info{k,4} = acc;
        
        k = k+1;
    end
    
    ACC
    display('Saving results');
    save_info = strcat('./run', run, '/test_info_ONEsvm.mat');
    sSave_info= char(save_info);
    save(sSave_info, 'test_info');
end
